<template>
  <div class="app-container">
    <!-- 查询条件区域-->
    <el-form :model="queryParams1" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="72px">
      <el-form-item label="摄像头ip" prop="ip">
        <el-input v-model="queryParams1.ip" placeholder="请输入摄像头ip" clearable @keyup.enter.native="handleQuery"
          size="small" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!--增删改查按钮区域-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['nssc:cameraConfig:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['nssc:cameraConfig:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['nssc:cameraConfig:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!--  数据列表区域-->
    <el-table v-loading="loading" :data="cameraConfigList" @selection-change="handleSelectionChange"
      :cell-style="{ borderColor: 'rgb(1 5 43)' }" :header-cell-style="{ borderColor: 'rgb(1 5 43)' }">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="布防状态" align="center" prop="state">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state == 1"
            style="font-size: 12px!important;height: 26px!important;line-height: 26px!important;display:inline-block!important;border-radius:4px!important;background: #20c013!important;border: 1px solid #024697!important;color: #fff!important">已布防</el-tag>
          <el-tag v-else
            style="font-size: 12px!important;height: 26px!important;line-height: 26px!important;display:inline-block!important;border-radius:4px!important;background:#6c6b6b!important;border: 1px solid #024697!important;color: #fff!important">未布防</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="摄像头ip" align="center" prop="ip" />
      <el-table-column label="摄像头端口" align="center" prop="port" />
      <el-table-column label="摄像头用户名" align="center" prop="user" />
      <el-table-column label="摄像头密码" align="center" prop="psw" />
      <el-table-column label="管廊编号" align="center" prop="poolNum">
        <template slot-scope="scope">
          <span>{{ scope.row.poolNum }}管廊</span>
        </template>
      </el-table-column>
      <el-table-column label="摄像头位置" align="center" prop="position">
        <template slot-scope="scope">
          <span>位于管廊{{ scope.row.position }}侧</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-circle-plus-outline" @click="handleBF(scope.row)">布防</el-button>
          <el-button size="mini" type="text" icon="el-icon-remove-outline" @click="handleCF(scope.row)">撤防</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['nssc:cameraConfig:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['nssc:cameraConfig:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--列表分页区域-->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" style="background:rgb(1 5 43);" />
    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" class="formStyle">
        <el-row>
          <el-col :span="12">
            <el-form-item label="摄像头ip" prop="ip">
              <el-input v-model="form.ip" placeholder="请输入摄像头ip" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="摄像头端口号" prop="port">
              <el-input v-model="form.port" @input="limitInput" placeholder="请输入摄像头端口号(默认8000)" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="摄像头用户名" prop="user">
              <el-input v-model="form.user" placeholder="请输入摄像头用户名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="摄像头密码" prop="psw">
              <el-input v-model="form.psw" placeholder="请输入摄像头密码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- <el-form-item label="摄像头所属池子" prop="poolNum">
              <el-input v-model="form.poolNum" placeholder="请输入所属池子编号" />
            </el-form-item> -->
            <el-form-item label="摄像头管廊编号" prop="poolNum">
              <el-select v-model="form.poolNum" placeholder="请选择管廊编号">
<!--                <el-option label="1#池子" value="1#"></el-option>-->
<!--                <el-option label="2#池子" value="2#"></el-option>-->
                <el-option label="3#" value="3#"></el-option>
                <el-option label="4#" value="4#"></el-option>
                <el-option label="5#" value="5#"></el-option>
                <el-option label="6#" value="6#"></el-option>
<!--                <el-option label="7#池子" value="7#"></el-option>-->
<!--                <el-option label="8#池子" value="8#"></el-option>-->
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- <el-form-item label="摄像头位置" prop="position">
              <el-input v-model="form.position" placeholder="请输入摄像头位置" />
            </el-form-item> -->
            <el-form-item label="摄像头位置" prop="position">
              <el-select v-model="form.position" placeholder="请选择摄像头方向">
                <el-option label="上" value="上"></el-option>
                <el-option label="下" value="下"></el-option>
                <el-option label="左" value="左"></el-option>
                <el-option label="右" value="右"></el-option>
                <el-option label="右上" value="右上"></el-option>
                <el-option label="右下" value="右下"></el-option>
                <el-option label="左上" value="左上"></el-option>
                <el-option label="左下" value="左下"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  addConfig,
  alarmBF,
  alarmCF,
  delConfig,
  getConfig,
  listConfig,
  updateConfig
} from "@/api/nssc/cameraConfig/cameraConfig";
export default {
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //复选删除时弹框内容
      ipNames: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 站点表格数据
      cameraConfigList: [],
      timer: null,//定时器
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams1: {
        pageNum: 1,
        pageSize: 8,
        ip: undefined
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 8,
        ip: undefined,
        user: undefined,
        port: undefined,
        psw: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ip: [
          { required: true, message: "摄像头ip不能为空", trigger: "blur" }
        ],
        user: [
          { required: true, message: "摄像头用户名不能为空", trigger: "blur" }
        ],
        port: [
          { required: true, message: "摄像头端口号不能为空", trigger: "blur" }
        ],
        psw: [
          { required: true, message: "摄像头密码不能为空", trigger: "blur" }
        ],
        poolNum: [
          { required: true, message: "摄像头管廊编号不能为空" }
        ],
        position: [
          { required: true, message: "摄像头位置不能为空" }
        ]
      }
    };
  },
  //初始化配置列表
  created() {
    this.getList();
  },
  mounted() {
      this.timer = setInterval(() => {
        this.getList(); //刷新站点列表
        console.log("机器人在线离线定时器")
      }, 6000);//6秒
    },
    beforeDestroy() {
      //清除定时器
      if (this.timer) {
        clearInterval(this.timer); //关闭
      }
    },
  methods: {
    limitInput() {
      // 使用正则表达式限制输入为四位数
      this.form.port = this.form.port.replace(/\D/g, '').slice(0, 4);
    },
    /** 查询布防摄像头列表 */
    getList() {
      this.loading = true;
      listConfig(this.queryParams1).then(response => {
        this.cameraConfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        ip: undefined,
        user: undefined,
        port: undefined,
        psw: undefined,
        poolNum: undefined,
        position: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.ipNames = selection.map(item => item.ip)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getConfig(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改配置";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      const ipNames = row.ip || this.ipNames
      this.$modal.confirm('是否确认删除ip名称为"' + ipNames + '"的数据项？').then(function () {
        // this.$modal.confirm('是否确认删除选中的数据项？').then(function() {
        return delConfig(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },

    /**
     * 摄像头布防
     */
    handleBF(row) {
      this.queryParams.ip = row.ip;
      this.queryParams.user = row.user;
      this.queryParams.port = row.port;
      this.queryParams.psw = row.psw;
      alarmBF(this.queryParams).then(response => {
        if (typeof (response) != null && response != "" && response.msg != "") this.$modal.msgError(response.msg);
      });
    },

    /**
     * 摄像头撤防
     */
    handleCF(row) {
      this.queryParams.ip = row.ip;
      alarmCF(this.queryParams).then(response => {
        if (typeof (response) != null && response != "" && response.msg != "") this.$modal.msgError(response.msg);
      });
    },
  }
};
</script>

<style lang="scss" scoped>
.formStyle{
  ::v-deep .el-input--medium .el-input__inner {
    width: 234px;
  }
}

</style>
